<?php

use App\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Faker\Generator as Faker;

class UserAreaSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $faker = app(Faker::class);
        $users = User::where('area_id', 0)->get();
        $province_ids = DB::table('china_area')->where('parent_id', 1)->pluck('id');
        $city_ids = DB::table('china_area')->whereIn('parent_id', $province_ids)->pluck('id');
        $area_ids = DB::table('china_area')->whereIn('parent_id', $city_ids)->pluck('code');
        foreach ($users as $user) {
            $user->area_id = $faker->randomElement($area_ids);
            $user->save();
        }
    }
}
